package com.stu.Demo;

import com.stu.entity.Dept;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author zy
 * @date 2025/3/3 15:40
 */
public class JDBCDemo4 {
    private static String url = "jdbc:mysql://localhost:3306/empdb?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    private static String username = "root";
    private static String password = "root";

    public static void main(String[] args) {
        Dept dept = new Dept(90,"客户端部门","苏州");
        Dept dept2 = new Dept(80,"服务端部门","南京");

        try {
//            int InsertRow = insertDept(dept);
//            int DeleteRow = deleteDept(dept);
            int UpdateRow = updateDept(dept2);

//            System.out.println(InsertRow);
//            System.out.println(DeleteRow);
            System.out.println(UpdateRow);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static int insertDept(Dept dept) throws ClassNotFoundException {
        String sql = "insert into dept values(?,?,?)";
        int row = 0;
        Class.forName("com.mysql.cj.jdbc.Driver");

        try(Connection conn = DriverManager.getConnection(url,username,password);
            PreparedStatement preparedStatement = conn.prepareStatement(sql)){

            preparedStatement.setInt(1,dept.getDeptno());
            preparedStatement.setString(2,dept.getDname());
            preparedStatement.setString(3, dept.getLoc());

            row =  preparedStatement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row;
    }

    private static int deleteDept(Dept dept) throws ClassNotFoundException {
        String sql = "delete from dept where deptno = ?";
        int row = 0;
        Class.forName("com.mysql.cj.jdbc.Driver");

        try(Connection conn = DriverManager.getConnection(url,username,password);
            PreparedStatement preparedStatement = conn.prepareStatement(sql)){

            preparedStatement.setInt(1,dept.getDeptno());

            row =  preparedStatement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row;
    }

    private static int updateDept(Dept dept) throws ClassNotFoundException {
        String sql = "update dept set dname =?,loc =? where deptno = ?";
        int row = 0;
        Class.forName("com.mysql.cj.jdbc.Driver");

        try(Connection conn = DriverManager.getConnection(url,username,password);
            PreparedStatement preparedStatement = conn.prepareStatement(sql)){

            preparedStatement.setString(1,dept.getDname());
            preparedStatement.setString(2, dept.getLoc());
            preparedStatement.setInt(3,dept.getDeptno());

            row =  preparedStatement.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row;
    }
}
